package com.shzu.jwc.ty.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.shzu.jwc.ty.common.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shzu.jwc.ty.bean.PeTestAppointment;
import com.shzu.jwc.ty.common.ExportExcelUtils;
import com.shzu.jwc.ty.common.Semester;
import com.shzu.jwc.ty.service.PeTestScoreservice;
import com.shzu.jwc.ty.service.PeTestservice;
import com.shzu.jwc.ty.service.StudentInfoservice;
import com.shzu.jwc.ty.service.SystemConfigservice;

@Controller
@RequestMapping("/jwc/petestscore")
public class JwcPeTestScorecontroller {

	@Autowired
	SystemConfigservice systemConfigservice;
	
	@Autowired
	StudentInfoservice studentInfoservice;
	
	@Autowired
	PeTestScoreservice peTestScoreservice;
	
	@Autowired
	PeTestservice peTestservice;
	
	@RequestMapping("/petestscore.action")
	public String getpetestscore(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model) {
		Page page=new Page(peTestScoreservice.countpetestscore(),pn);
		page.setPageSize(20);
		List<PeTestAppointment> data=peTestScoreservice.getpetestscore(page);
		model.addAttribute("college", studentInfoservice.getcollege());
		model.addAttribute("data", data);
		model.addAttribute("page",page);
		model.addAttribute("year", studentInfoservice.getyear());
		model.addAttribute("date", Semester.getnow());
		model.addAttribute("code", systemConfigservice.getvalue("physicalfitness"));
		model.addAttribute("name", peTestservice.getpetest());
		return "jwc/petestscore/petestscore";
	}
	
	@RequestMapping("/selectpetestscore.action")
	public String selectpetestscore(@RequestParam("userStudentInfoYear") Integer userStudentInfoYear,@RequestParam("userStudentInfoCollege") String userStudentInfoCollege,@RequestParam(value="pn",defaultValue="1")Integer pn,Model model,HttpSession session) {
		if(userStudentInfoYear==0&&userStudentInfoCollege.equals("0")) {
			Page page=new Page(peTestScoreservice.countpetestscore(),pn);
			page.setPageSize(20);
			List<PeTestAppointment> data=peTestScoreservice.getpetestscore(page);
			model.addAttribute("data", data);
			model.addAttribute("page",page);
		}else {
			Page page=new Page(peTestScoreservice.countpetestscore(),pn);
			page.setPageSize(20);
			List<PeTestAppointment> data=peTestScoreservice.selectpetestscore(userStudentInfoYear,userStudentInfoCollege,page);
			model.addAttribute("data", data);
			model.addAttribute("page",page);
		}
		model.addAttribute("college", studentInfoservice.getcollege());
		model.addAttribute("year", studentInfoservice.getyear());
		model.addAttribute("date", Semester.getnow());
		model.addAttribute("code", systemConfigservice.getvalue("physicalfitness"));
		model.addAttribute("name", peTestservice.getpetest());
		session.setAttribute("userStudentInfoYear", userStudentInfoYear);
		session.setAttribute("userStudentInfoCollege", userStudentInfoCollege);
		return "jwc/petestscore/petestscore";
	}
	
	@RequestMapping("/searchpetestscore.action")
	public String searchpetestscore(@RequestParam("userStudentInfoName")String userStudentInfoName,@RequestParam(value="pn",defaultValue="1")Integer pn,Model model) {
		List<PeTestAppointment> data=peTestScoreservice.searchpetestscore(userStudentInfoName);
		model.addAttribute("college", studentInfoservice.getcollege());
		model.addAttribute("data", data);
		model.addAttribute("year", studentInfoservice.getyear());
		model.addAttribute("date", Semester.getnow());
		model.addAttribute("code", systemConfigservice.getvalue("physicalfitness"));
		model.addAttribute("name", peTestservice.getpetest());
		return "jwc/petestscore/petestscore";
	}
	
	@RequestMapping("/export.action")
	public ModelAndView export(@RequestParam("userStudentInfoYear") Integer userStudentInfoYear,@RequestParam("userStudentInfoCollege") String userStudentInfoCollege,@RequestParam("name")Integer peTestId,HttpServletResponse response) {
		try {
			List<PeTestAppointment> data=peTestScoreservice.upselectpetestscore(userStudentInfoYear, userStudentInfoCollege, peTestId);
			String code=systemConfigservice.getvalue("physicalfitness");
			PeTestAppointment appointment=new PeTestAppointment();
			String title="体能测试考试成绩";
			String[] rowsName=new String[] {"取得学年","取得学期","学号","课程／环节代码","课程类别一","课程类别二","课程类别三","环节类别","修读性质","考核方式","辅修标记","补考标记","特殊情况标记","成绩表现形式","原始成绩",
					"授课方式","等级","平时成绩","平时成绩表现形式","平时成绩百分比","期中成绩","期中成绩表现形式","期中成绩百分比","期末成绩"};
			List<Object[]> dataList =new ArrayList<Object[]>();
			Object[] objs=null;

			for (int i = 0; i < data.size(); i++) {
				PeTestAppointment po =null;
				po=data.get(i);
				objs = new Object[rowsName.length];
				String[] xq=Semester.getnow().split("-");
				objs[0] = xq[0];
				objs[1] = Integer.parseInt(xq[2])==1?"第一学期":"第二学期";
				objs[2] = po.getUserStudentInfo().getUserStudentInfoNum();
				objs[3] = code;
				objs[4] = "必修课";
				objs[5]="";
				objs[6]="";
				objs[7]="";
				objs[8] = "初修";
				objs[9] = "考试";
				if(po.getPeTestAppointmentFinal()==null)
					objs[12] = "缺考";
				else
					objs[12]=null;
				objs[14] = po.getPeTestAppointmentFinal();
				objs[17] = po.getPeTestAppointmentUsual();
				objs[23] = po.getPeTestAppointmentScore();
				dataList.add(objs);
			}
			ExportExcelUtils ex =new ExportExcelUtils(title, rowsName, dataList,response);
			ex.exportData();
		}catch(Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
